package com.quectel.cms.controller.partyeducation;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.partyeducation.dto.PartyEducationLibraryUserDto;
import com.quectel.core.module.partyeducation.service.PartyEducationLibraryUserService;
import com.quectel.util.common.*;
import com.quectel.util.kit.PageFetchKit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/07/06 13:30
 */
@RestController
@RequestMapping("partyEducationLibraryUser")
@Tag(name = "PartyEducationLibraryUserController", description = "党建文库人员关系相关api")
public class PartyEducationLibraryUserController extends BaseController {

    @DubboReference
    private PartyEducationLibraryUserService partyEducationLibraryUserService;

    /**
     * 导出文库访问人员
     */
    @GetMapping("exportExcel")
    @RequirePermissions(values = "partyEducationTrainingExam:export")
    @Operation(summary = "导出考试人员列表")
    public void exportPersonExcel(@RequestParam Map<String, Object> params,
                                  HttpServletResponse resp) {
        super.fillAuthorityData(params);

        List<PartyEducationLibraryUserDto> list = PageFetchKit.fetchAll(req -> partyEducationLibraryUserService.queryList(req), params, 500);
        String[] headers = new String[]{"所属党组织", "人员名称", "访问时间"};
        List<String[]> dataList = new ArrayList<>();
        for (PartyEducationLibraryUserDto data : list) {
            String[] item = new String[headers.length];
            item[0] = Objects.nonNull(data.getParty()) ? data.getParty().getName() : "";
            item[1] = data.getUsername();
            item[2] = DateUtils.format(data.getCheckTime(), DateUtils.FormatType.COMMON);
            dataList.add(item);
        }
        POIUtils.exportExcel(resp, "文库访问情况", headers, dataList, "文库访问情况");

    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "partyEducationLibrary:list")
    @Operation(summary = "分页获取党建文库人员关系列表")
    public Response<Page<PartyEducationLibraryUserDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = partyEducationLibraryUserService.queryTotal(request.getParams());

        List<PartyEducationLibraryUserDto> list = null;
        if (total > 0) {
            list = partyEducationLibraryUserService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<PartyEducationLibraryUserDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PartyEducationLibraryUserDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "partyEducationLibraryUser:edit")
    @Operation(summary = "在编辑时根据id查询党建文库人员关系详情")
    public Response<PartyEducationLibraryUserDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyEducationLibraryUserService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyEducationLibraryUserDto partyEducationLibraryUserDto = partyEducationLibraryUserService.selectById(id);

        return Response.<PartyEducationLibraryUserDto>ok().wrap(partyEducationLibraryUserDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "partyEducationLibraryUser:info")
    @Operation(summary = "根据id查询党建文库人员关系的详情")
    public Response<PartyEducationLibraryUserDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyEducationLibraryUserService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyEducationLibraryUserDto partyEducationLibraryUserDto = partyEducationLibraryUserService.selectById(id);

        return Response.<PartyEducationLibraryUserDto>ok().wrap(partyEducationLibraryUserDto);
    }

    /**
     * 保存
     *
     * @param partyEducationLibraryUserDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "partyEducationLibraryUser:edit")
    @Operation(summary = "新增党建文库人员关系")
    public Response<Object> save(@RequestBody @Validated PartyEducationLibraryUserDto partyEducationLibraryUserDto) {

        //填充基础数据
        fillSaveCommonData(partyEducationLibraryUserDto);

        partyEducationLibraryUserService.save(partyEducationLibraryUserDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param partyEducationLibraryUserDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "partyEducationLibraryUser:edit")
    @Operation(summary = "根据id修改党建文库人员关系")
    public Response<Object> update(@RequestBody  @Validated PartyEducationLibraryUserDto partyEducationLibraryUserDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(partyEducationLibraryUserDto.getId()));
        if (partyEducationLibraryUserService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(partyEducationLibraryUserDto);

        partyEducationLibraryUserService.updateById(partyEducationLibraryUserDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "partyEducationLibraryUser:delete")
    @Operation(summary = "根据多条记录id批量删除党建文库人员关系")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (partyEducationLibraryUserService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        partyEducationLibraryUserService.deleteBatch(ids);

        return Response.ok();
    }
}
